package com.yanxu.set;

import com.yanxu.common.SnmpParameter;
import com.yanxu.inter.SnmpRequestHandle;
import org.snmp4j.PDU;
import org.snmp4j.ScopedPDU;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.*;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2023/11/9-2023
 * @Description: Snmp set 请求
 */
@Component("snmp-set")
public class SnmpSetRequest extends SnmpRequestHandle {

    @Override
    public List<PDU> beforeRequest(SnmpParameter snmpParameter) throws Exception {
        List<PDU> pduList = new ArrayList<>();
        PDU pdu = (snmpParameter.getVersion() == SnmpConstants.version3) ? (new ScopedPDU()) : (new PDU());
        pdu.setType(PDU.SET);
        String[] oids = snmpParameter.getOids();
        String[] variables = snmpParameter.getVariables();
        String[] types = snmpParameter.getTypes();
        for (int i = 0; i < oids.length; ++i) {
            String oid = oids[i];
            String variable = variables[i];
            String type = types[i];
            /*
              tyyw 中是在 xml 中定义好 要设置的值，以及值的类型
              <Parameter type="" key="Variable" value="1"/>
              <Parameter type="" key="Type" value="Integer32"/>
              new VariableBinding( OID, variable)   variable=  OctetString、Counter64、Counter32、Integer32 ...
             */
            VariableBinding variableBinding = new VariableBinding(new OID(oid));
            switch (type) {
                case "OctetString":
                    variableBinding.setVariable(new OctetString(variable));
                    break;
                case "Counter32":
                    variableBinding.setVariable(new Counter32(Long.parseLong(variable)));
                    break;
                case "Counter64":
                    variableBinding.setVariable(new Counter64(Long.parseLong(variable)));
                    break;
                case "Integer32":
                    variableBinding.setVariable(new Integer32(Integer.parseInt(variable)));
                    break;
                case "Gauge32":
                    variableBinding.setVariable(new Gauge32(Long.parseLong(variable)));
                    break;
                default:
                    variableBinding.setVariable(new OctetString(variable));
                    break;
            }
            pdu.add(variableBinding);
            pduList.add(pdu);
        }
        return pduList;
    }

    @Override
    public void handleResult(List<Object> result) {

    }
}